/*********************************************************************************
 *      Copyright:  (C) 2023 iot23<11>
 *                  All rights reserved.
 *
 *       Filename:  pack_data.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(20/04/23)
 *         Author:  iot23 <11>
 *      ChangeLog:  1, Release initial version on "20/04/23 11:04:25"
 *                 
 ********************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "get_time.h"
#include "ds18b20.h"
#include "pack_data.h"
#include "iniparser.h"
#include "dictionary.h"
#include "cJSON.h"

int get_mqttdata(char *ini_path,data_mqtt *mqtt, int option)
{
	const char      *host = NULL;
	const char      *username = NULL;
	const char      *passwd = NULL;
	const char      *clientid = NULL;
	const char      *topic = NULL;
	const char      *identifier = NULL;
	const char      *method = NULL;
	const char      *jsonid = NULL;
	const char      *version = NULL;
	int              port = 0; 
	int              Qos = 0;
	dictionary 		*ini = NULL;
	
	if( !ini_path || !mqtt )
	{
		printf("error\n");
		return -1;
	}


	ini = iniparser_load(ini_path);
	if( !ini )
	{
		printf("parsing failure!\n");
		return -1;
	}
 
	if( option == 1 )//aliyun
	{
		host = iniparser_getstring(ini, "al_connect:host", NULL);
		port = iniparser_getint(ini,"al_connect:port",1883);
		username = iniparser_getstring(ini,"al_connect:username",NULL);
		passwd = iniparser_getstring(ini,"al_connect:passwd",NULL);
		clientid = iniparser_getstring(ini,"al_connect:clientid",NULL);
		topic = iniparser_getstring(ini,"al_send:topic", NULL);
		method = iniparser_getstring(ini,"al_send:method", NULL);
		jsonid = iniparser_getstring(ini,"al_send:id", NULL);
		version = iniparser_getstring(ini,"tx_send:version", NULL);
	}
	else if( option == 2 )//tenxunyun
	{
		host = iniparser_getstring(ini, "tx_connect:host", NULL);
		port = iniparser_getint(ini,"tx_connect:port",1883);
		username = iniparser_getstring(ini,"tx_connect:username",NULL);
		passwd = iniparser_getstring(ini,"tx_connect:passwd",NULL);
		clientid = iniparser_getstring(ini,"tx_connect:clientid",NULL);
		method = iniparser_getstring(ini,"tx_send:update", NULL);
		topic = iniparser_getstring(ini,"tx_send:topic", NULL);
		jsonid = iniparser_getstring(ini,"tx_send:clientToken", NULL);
		version = iniparser_getstring(ini,"tx_send:version", NULL);
	}
	else if( option == 3 )//huaweiyun
	{
		host = iniparser_getstring(ini, "hw_connect:host", NULL);
		port = iniparser_getint(ini,"hw_connect:port",1883);
		username = iniparser_getstring(ini,"hw_connect:username",NULL);
		passwd = iniparser_getstring(ini,"hw_connect:passwd",NULL);
		clientid = iniparser_getstring(ini,"hw_connect:clientid",NULL);
		method = iniparser_getstring(ini,"hw_send:value", "11");
		topic = iniparser_getstring(ini,"hw_send:topic", NULL);
		jsonid = iniparser_getstring(ini,"al_send:id", NULL);
		version = iniparser_getstring(ini,"tx_send:version", NULL);
	}

	identifier = iniparser_getstring(ini,"common:identifier",NULL);
	Qos = iniparser_getint(ini,"common:Qos",0);
			
	
	mqtt->Qos=Qos;
	strncpy(mqtt->hostname, host,sizeof(mqtt->hostname));
	mqtt->port = port;
	strncpy(mqtt->username, username,sizeof(mqtt->username));
	strncpy(mqtt->passwd, passwd,sizeof(mqtt->passwd));
	strncpy(mqtt->clientid, clientid,sizeof(mqtt->clientid));
	strncpy(mqtt->topic, topic,sizeof(mqtt->topic));
	strncpy(mqtt->method, method,sizeof(mqtt->method));
	strncpy(mqtt->identifier, identifier,sizeof(mqtt->identifier));
	strncpy(mqtt->jsonid, jsonid,sizeof(mqtt->jsonid));
	strncpy(mqtt->version, version,sizeof(mqtt->version));
    
}

int pack_data(data_mqtt *mqtt,char *msg,int size, int option)
{
	char			*msg1 = NULL;
	float			 temp;
	char			 time[64];
	
	if( !mqtt || !msg )
	{
		printf("Parameter error!\n");
		return -1;
	}

	if( get_temp(&temp))
	{
		return -1;
	}

	memset(time,0,sizeof(time));
	get_time(time, sizeof(time));

	cJSON   * root =  cJSON_CreateObject();
	cJSON   * item3 =  cJSON_CreateArray();
	cJSON   * item1 =  cJSON_CreateObject();
	cJSON   * item2 =  cJSON_CreateObject();
   
	memset(root,0,sizeof(root));
	memset(item1,0,sizeof(item1));
	memset(item2,0,sizeof(item2));
	memset(item3,0,sizeof(item3));

	if( option == 3)
	{
		cJSON_AddItemToObject(root, "services",item3);
		cJSON_AddItemToObject(item1, "service_id",cJSON_CreateString(mqtt->method));
		cJSON_AddItemToObject(item1, "properties",item2);
		cJSON_AddItemToObject(item2, mqtt->identifier , cJSON_CreateNumber(temp));
		cJSON_AddItemToArray(item3, item1);
	}
	else if( option == 2)
	{  
		cJSON_AddItemToObject(root, "type", cJSON_CreateString(mqtt->method));
		cJSON_AddItemToObject(root, "state",item1);
		cJSON_AddItemToObject(item1,"reported",item2);
		cJSON_AddItemToObject(item2, mqtt->identifier, cJSON_CreateNumber(temp));
		cJSON_AddItemToObject(root, "version", cJSON_CreateString(mqtt->version));
		cJSON_AddItemToObject(root, "clientToken", cJSON_CreateString(mqtt->jsonid));
	}
	else if( option == 1)
	{
		cJSON_AddItemToObject(root, "method", cJSON_CreateString(mqtt->method));
		cJSON_AddItemToObject(root, "jsonid", cJSON_CreateString(mqtt->jsonid));
		cJSON_AddItemToObject(root, "params",item1);
		cJSON_AddItemToObject(root, "time",cJSON_CreateString(time));
		cJSON_AddItemToObject(item1, mqtt->identifier, cJSON_CreateNumber(temp));
		cJSON_AddItemToObject(root, "version", cJSON_CreateString(mqtt->version));
	}
	
	msg1=cJSON_Print(root);
	strncpy(msg , msg1 , size);
	free(msg1);

	return 0;
}


